package com.cw.DesignModel23.Ceational.PrototypePattern.clone2;

public class Client {

    public static void main(String[] args) {
        System.out.println("原型模式完成对象的创建");
        Sheep sheep = new Sheep("tom", 1, "白色");
        sheep.friend = new Sheep("jack", 2, "黑色");

        Sheep sheep2 = (Sheep)sheep.clone();
        Sheep sheep3 = (Sheep)sheep.clone();
        Sheep sheep4 = (Sheep)sheep.clone();
        Sheep sheep5 = (Sheep)sheep.clone();

        System.out.println("sheep2 =" + sheep2 + "sheep2.friend=" + sheep2.friend.hashCode());
        System.out.println("sheep3 =" + sheep3 + "sheep3.friend=" + sheep3.friend.hashCode());
        System.out.println("sheep4 =" + sheep4 + "sheep4.friend=" + sheep4.friend.hashCode());
        System.out.println("sheep5 =" + sheep5 + "sheep5.friend=" + sheep5.friend.hashCode());
        /**
         * 原型模式完成对象的创建
         * sheep2 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep2.friend=1554874502
         * sheep3 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep3.friend=1554874502
         * sheep4 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep4.friend=1554874502
         * sheep5 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep5.friend=1554874502
         *
         * Process finished with exit code 0
         */
    }
}

/**
 * 【分析】
 *
 * 根据输出，可以看到其他sheep的friend和原型对象的friend是同一个对象（因为hashcode一样），
 * 原因：上面的代码是浅拷贝，浅拷贝并不会重新复制一个引用类型的对象出来，只是单纯将克隆对象的friend指向原型对象的friend对象，
 * 这样的坏处是，如果原型对象的friend发生了改变，克隆对象的friend也会改变，这样不能算是真正的克隆
 * ————————————————
 * 版权声明：本文为CSDN博主「Hello Dam」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
 * 原文链接：https://blog.csdn.net/laodanqiu/article/details/131853772
 */
